﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
using System.IO;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 违章记录编辑
    /// </summary>
    public partial class frmAccidentEdit : FormBase, IPopupFormEdit {
        SortableSearchableBindingList<Vehicles> m_Act = new SortableSearchableBindingList<Vehicles>();

        public frmAccidentEdit()
        {
            InitializeComponent();
        }
        void dataBind() {
            this.txtVehicleNumber.DataBindings.Add("EditValue", rowData, "VehicleNumber");
            this.txtVehType.DataBindings.Add("EditValue", rowData, "VehType");
            this.txtEmpolyee_Name.DataBindings.Add("EditValue", rowData, "Empolyee_Name");
            this.txtAct_Code.DataBindings.Add("EditValue", rowData, "Act_Code");
            this.txtAct_Name.DataBindings.Add("EditValue", rowData, "Act_Name");
            this.txtAccountCode.DataBindings.Add("EditValue", rowData, "AccountCode");
            this.txtAccountTitle.DataBindings.Add("EditValue", rowData, "AccountTitle");
            this.txtOperDate.DataBindings.Add("EditValue", rowData, "OperDate");

            this.txtSite.DataBindings.Add("EditValue", rowData, "Site");
            this.txtConfirmUser.DataBindings.Add("EditValue", rowData, "ConfirmUser");
            this.txtDescription.DataBindings.Add("EditValue", rowData, "Description");
            this.txtDamage.DataBindings.Add("EditValue", rowData, "Damage");
            this.txtContent.DataBindings.Add("EditValue", rowData, "Content");
            this.txtName2.DataBindings.Add("EditValue", rowData, "Name2");
            this.txtTel2.DataBindings.Add("EditValue", rowData, "Tel2");
            this.txtBOrgName.DataBindings.Add("EditValue", rowData, "BOrgName");
            this.txtVehicleNumber2.DataBindings.Add("EditValue", rowData, "VehicleNumber2");
            this.txtVehType2.DataBindings.Add("EditValue", rowData, "VehType2");
            this.txtDamage2.DataBindings.Add("EditValue", rowData, "Damage2");
            this.txtCompanyNum.DataBindings.Add("EditValue", rowData, "CompanyNum");
            this.txtOtherNum.DataBindings.Add("EditValue", rowData, "OtherNum");
            this.txtInsuranceNum.DataBindings.Add("EditValue", rowData, "InsuranceNum");
            this.txtCheckUser.DataBindings.Add("EditValue", rowData, "CheckUser");
            this.txtRemark.DataBindings.Add("EditValue", rowData, "Remark");
            this.picImage.DataBindings.Add("EditValue", rowData, "Image");
            
        }
        #region IPopupFormEdit Members
        private VehicleAccident rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehicleAccident;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehicleAccident>(value as VehicleAccident, rowData);
                    InitComboBoxData();
                }
            }
        }

        #endregion

        private void InitComboBoxData() {
            IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='事故'");
            if(list!=null && list.Count>0){
                rowData.Act_Code = list[0].Act_Code;
                rowData.Act_Name = list[0].Act_Name;
            }

            IList<Vehicles> vlist = MainHelper.GetSqlMap<Vehicles>().GetListByWhere<Vehicles>(" order by VehicleNumber");
            txtVehicleNumber.Properties.DataSource = vlist;
            IList<Accounts> acclist = MainHelper.GetSqlMap<Accounts>().GetListByWhere<Accounts>(" where DebitOrCredit like '%行政%' order by AccountCode");
            txtAccountTitle.Properties.DataSource = acclist;
            IList<VehicleDriverLicense> drvlist = MainHelper.GetSqlMap<VehicleDriverLicense>().GetListByWhere<VehicleDriverLicense>("  order by Empolyee_Name");
            //txtEmpolyee_Name.Properties.DataSource = drvlist;
            if (rowData.VehicleNumber == "")
            {
                rowData.OperDate = DateTime.Now;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="comboBox"></param>
        /// <param name="displayMember"></param>
        /// <param name="valueMember"></param>
        /// <param name="nullTest"></param>
        /// <param name="cnStr"></param>
        /// <param name="post"></param>
        public void SetComboBoxData(DevExpress.XtraEditors.LookUpEdit comboBox, string displayMember, string valueMember, string nullTest, string cnStr, SortableSearchableBindingList<Vehicles> post)
        {
            comboBox.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            comboBox.Properties.DataSource = post;
            comboBox.Properties.DisplayMember = displayMember;
            comboBox.Properties.ValueMember = valueMember;
            comboBox.Properties.NullText = nullTest;
            comboBox.Properties.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(valueMember, "VehicleNumber", 20, DevExpress.Utils.FormatType.None, "", false, DevExpress.Utils.HorzAlignment.Default),
            new DevExpress.XtraEditors.Controls.LookUpColumnInfo(displayMember, cnStr)});
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtVehicleNumber.Text == "")
            {
                MessageBox.Show("车牌号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtVehicleNumber.Focus();
                return;
            }
            if (txtAccountTitle.Text == "")
            {
                MessageBox.Show("科目名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAccountTitle.Focus();
                return;
            }
            if (txtSite.Text == "")
            {
                MessageBox.Show("事故发生地点不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSite.Focus();
                return;
            }
            if (txtDescription.Text == "")
            {
                MessageBox.Show("事故概要不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtDescription.Focus();
                return;
            }

            if (picImage.Image == null)
            {
                rowData.Image = new byte[0];
            }
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtAccountTitle_EditValueChanged(object sender, EventArgs e)
        {
            if (txtAccountTitle.Text != "")
            {
                rowData.AccountCode = ((Accounts)txtAccountTitle.Properties.GetDataSourceRowByKeyValue(txtAccountTitle.Text)).AccountCode;
                txtAccountCode.Text = rowData.AccountCode;
            }
        }

        private void groupControlOrg_Paint(object sender, PaintEventArgs e)
        {

        }

        private void txtVehicleNumber_EditValueChanged(object sender, EventArgs e)
        {
            if (txtVehicleNumber.Text != "")
            {
                rowData.VehType = ((Vehicles)txtVehicleNumber.Properties.GetDataSourceRowByKeyValue(txtVehicleNumber.Text)).VehicleType;
                rowData.Empolyee_Name = ((Vehicles)txtVehicleNumber.Properties.GetDataSourceRowByKeyValue(txtVehicleNumber.Text)).EmployeeName;
                txtVehType.Text = rowData.VehType;
                txtEmpolyee_Name.Text = rowData.Empolyee_Name;
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string filepath = "";
            openFileDialog1.Filter = "图片文件|*.jpeg;*.jpg;*.bmp;*.gif;*.png";
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                filepath = openFileDialog1.FileName;
                FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                Byte[] btye2 = new byte[fs.Length];
                fs.Read(btye2, 0, Convert.ToInt32(fs.Length));
                picImage.Image = Image.FromStream(fs);
                rowData.Image = btye2;      
            }
        }

        private void txtVehicleNumber_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmVehiclesSeletcter frm = new frmVehiclesSeletcter();
            frm.InitData_Slect("");
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtVehicleNumber.Text = frm.SelectObj.VehicleNumber;
                txtVehicleNumber.ClosePopup();
            }
        }
    }
}